package free.emaranhado.lineparams.util;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import free.emaranhado.lineparams.model.ConductorTypes;

public class XMLParser extends DefaultHandler {
	Boolean currentElement = false;
	String currentValue = null;
	public static ConductorTypes CondTypes = null;

	public static ConductorTypes getSitesList() {
		return CondTypes;
	}

	public static void setSitesList(ConductorTypes sitesList) {
		XMLParser.CondTypes = sitesList;
	}

	/**
	 * Called when tag starts ( ex:- <name>AndroidPeople</name> -- <name> )
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		currentElement = true;

		if (localName.equals("Table")) {
			/** Start */
			CondTypes = new ConductorTypes();
		}
	}

	/**
	 * Called when tag closing ( ex:- <name>AndroidPeople</name> -- </name> )
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		currentElement = false;

		/** set value */
		if (localName.equalsIgnoreCase("code_word"))
			CondTypes.setCode_word(currentValue);
		else if (localName.equalsIgnoreCase("GMR"))
			CondTypes.setGMR(currentValue);
		else if (localName.equalsIgnoreCase("Rac_20"))
			CondTypes.setRac_20(currentValue);

	}

	/**
	 * Called to get tag characters ( ex:- <name>AndroidPeople</name> -- to get
	 * AndroidPeople Character )
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		if (currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}

	}
}
